﻿using SchoolCommunicationSystem.Models;
using SCS.Business;
using SCS.Common.Models;
using System.Collections.Generic;
using System.Web.Mvc;

namespace SchoolCommunicationSystem.Controllers
{
    //[Authorize]
    public class SchoolYearsController : Controller
    {
        private SchoolYearServices _schoolYearServices = new SchoolYearServices();

        /// <summary>
        /// Pre add a new school year
        /// </summary>
        /// <returns>View</returns>
        public ActionResult AddSchoolYear()
        {
            IEnumerable<SchoolYear> schoolYears;
            schoolYears = _schoolYearServices.GetAll();

            var data = new SchoolYearsModel()
            {
                SchoolYears = schoolYears
            };
            return View(data);
        }

        /// <summary>
        /// add a new school year
        /// </summary>
        /// <param name="schoolYears">SchoolYearsModel</param>
        /// <returns>View</returns>
        [HttpPost]
        public ActionResult AddSchoolYear(SchoolYearsModel schoolYears)
        {
            if (ModelState.IsValid)
            {
                SchoolYear sy = new SchoolYear();
                sy.schoolYearName = schoolYears.SchoolYearName;
                sy.startDate = schoolYears.startDate;
                sy.endDate = schoolYears.endDate;
                if (_schoolYearServices.CheckSchoolYearName(sy))
                {
                    ChangeClassModel changeClass = new ChangeClassModel();

                    changeClass.oldSchoolYearsID = _schoolYearServices.GetMaxSchoolYear();
                    var schoolYearlast = _schoolYearServices.GetSchoolYear_Last();
                    changeClass.oldSchoolYearsName = schoolYearlast.schoolYearName;
                    changeClass.newSchoolYearsName = schoolYears.SchoolYearName;
                    changeClass.schoolYearsID = changeClass.oldSchoolYearsID + 1;

                    _schoolYearServices.AddSchoolYear(sy);
                    return RedirectToAction("ChangeClasses", "SchoolYears", changeClass);
                  //  return RedirectToAction("AddSchoolYear");
                }
                else
                {
                    IEnumerable<SchoolYear> SY;
                    SY = _schoolYearServices.GetAll();

                    var data = new SchoolYearsModel()
                    {
                        SchoolYears = SY
                    };

                    ModelState.AddModelError(string.Empty, "Dữ liệu nhập đã tồn tại hoặc nhập không đúng");
                    return View(data);
                }
            }
            IEnumerable<SchoolYear> schoolYear;
            schoolYear = _schoolYearServices.GetAll();
            schoolYears.SchoolYears = schoolYear;

            return View(schoolYears);
        }


        /// <summary>
        /// Pre add a new school year
        /// </summary>
        /// <returns>View</returns>
        public ActionResult ChangeClasses(ChangeClassModel model)
        {
            List<ClassInSchoolYear> classInSchoolYears = _schoolYearServices.GetAllClassInASchoolYear(model.oldSchoolYearsID);
            model.ClassInNewSchoolYear = new List<ClassInNewSY>(); 
            foreach (ClassInSchoolYear item in classInSchoolYears)
            {
               
                int classNameFirst = 0;
                int.TryParse(item.className.Substring(0,1), out classNameFirst);
                if (int.TryParse(item.className.Substring(0, 1), out classNameFirst) && classNameFirst < 9 && classNameFirst > 5)
                {
                    ClassInNewSY _class = new ClassInNewSY();
                    _class.classID = item.classID;
                    _class.className = item.className;
                    _class.NewClassName = item.className.Substring(1).Insert(0,(classNameFirst+1).ToString());
                    model.ClassInNewSchoolYear.Add(_class);
                }
            }
            return View(model);
        }
        [HttpPost]
        public ActionResult ChangeClassesSY(ChangeClassModel model)
        {
            List<ClassInSchoolYear> newClass = new List<ClassInSchoolYear>();
            foreach (var item in model.ClassInNewSchoolYear)
            {
                ClassInSchoolYear _class = new ClassInSchoolYear();
                _class.classID = item.classID;
                _class.className = item.NewClassName;
                _class.schoolYearID = model.schoolYearsID;
                newClass.Add(_class);
            }
            _schoolYearServices.ChangeClassInNewSchoolYear(newClass);
            return RedirectToAction("CLassList", "Class", new { schoolYearIDList = model.schoolYearsID });
        }
    }
}
